# -*- coding: utf-8 -*-
import mechanize
from mechanize import Browser
import re
import cStringIO as StringIO
import lxml.html
from lxml import etree
import json
from urllib import urlencode
import zlib
import datetime
import sys
import cherrypy

# <!--- basic function --
def getCRC32Unsign(textToHash=None):
    return str(zlib.crc32(textToHash) & 0xffffffffL)

def buildTree(url, ishtml=False):
    ua6300 = "Nokia6300/2.0 (04.20) Profile/MIDP-2.0 Configuration/CLDC-1.1 UNTRUSTED/1.0"
    br = Browser()
    br.addheaders = [("User-Agent", ua6300)]
    br.open(url)
    html = br.response().read()
    if ishtml: print html
    parser = etree.HTMLParser(encoding='utf-8')
    tree = etree.parse(StringIO.StringIO(html), parser)
    return tree

def getElementText(elem):
    if elem is None: return ''
    t = lxml.html.fromstring(etree.tostring(elem))
    return t.text_content().strip()

def getAttributeText(node, attrb):
    if node is None or attrb == '': return ''
    return node.get(attrb)

def extractWithRegEx(pat, matchStr, matchIdx):
    ''' Hàm chạy regex và trả kết quả ở group[matchIdx]. Nếu không có kết quả -> trả về null'''
    try:
        result = ''
        rexp = re.compile(pat)
        m = rexp.search(matchStr)
        if (m!=''):
            result = m.group(matchIdx)
        return result
    except:
        return ''

def strToASCII(str):
    if str == '': return ''
    try:
        listPattern = [r"á|à|ả|ạ|ã|â|ấ|ầ|ẩ|ậ|ẫ|ă|ắ|ằ|ẳ|ặ|ẵ", r"Á|À|Ả|Ạ|Ã|Â|Ấ|Ầ|Ẩ|Ậ|Ẫ|Ă|Ắ|Ằ|Ẳ|Ặ|Ẵ",
                       r"đ", r"Đ", r"í|ì|ỉ|ị|ĩ", r"Í|Ì|Ỉ|Ị|Ĩ", r"é|è|ẻ|ẹ|ẽ|ê|ế|ề|ể|ệ|ễ", r"É|È|Ẻ|Ẹ|Ẽ|Ê|Ế|Ề|Ể|Ệ|Ễ",
                       r"ó|ò|ỏ|ọ|õ|ô|ố|ồ|ổ|ộ|ỗ|ơ|ớ|ờ|ở|ợ|ỡ", r"Ó|Ò|Ỏ|Ọ|Õ|Ô|Ố|Ồ|Ổ|Ộ|Ỗ|Ơ|Ớ|Ờ|Ở|Ợ|Ỡ",
                       r"ú|ù|ủ|ụ|ũ|ư|ứ|ừ|ử|ự|ữ", r"Ú|Ù|Ủ|Ụ|Ũ|Ư|Ứ|Ừ|Ử|Ự|Ữ", r"ý|ỳ|ỷ|ỵ|ỹ", r"Ý|Ỳ|Ỷ|Ỵ|Ỹ"]
        rep = ['a', 'A', 'd', 'D', 'i', 'I', 'e', 'E', 'o', 'O', 'u', 'U', 'y', 'Y']
        str = str.encode('utf-8', 'replace')
        for idx in range(len(listPattern)):
            str = re.sub(listPattern[idx], rep[idx], str)
        return str
    except:
        pass

# -- end basic function --!>

def compareURL(curURL, nextURL):
    curStart = extractWithRegEx("start=(\d+)", curURL, 1)
    nextStart = extractWithRegEx("start=(\d+)", nextURL, 1)
    print "\n\tcurstart {0} -- nextstart {1}".format(curStart, nextStart)
    if curStart == nextStart:   return True
    return False


class ImagesGoogle():
    
    @cherrypy.expose
    def getimg(self, q, pg):
        url = "http://images.google.com/m?{0}".format(urlencode({'q': q}))
        print url
        if pg == '' or int(pg) < 1: pg = 1
        try:
            results = []
            iPage = 0
            while 1:
                tree = buildTree(url)
                if len(tree.xpath("//div[@id='navbar']/following-sibling::*[1]/div//img[@class]")) < 1:
                    print "!!! Non content :("
                    break
                iPage += 1
                print iPage, " : ", pg
                if iPage > int(pg): break
                elif iPage == int(pg):
                    for item in tree.xpath("//div[@id='navbar']/following-sibling::*[1]/div//img[@class]"):
                        gLink = getAttributeText(item, 'src')
                        rLink = "http://{0}".format(extractWithRegEx(r"::(.+\.(jpg|jpe|jpeg|png|gif|bmp|JPG|JPE|JPEG|PNG|GIF|BMP))", gLink, 1))
                        print gLink
                        print rLink
                        print '-------------------'
                        results.append({'GoogleLink': gLink, 'RealLink': rLink})
                    return json.dumps(results)
                tiepNode = tree.xpath("//a[contains(., 'Trang tiếp')]".decode('utf-8'))
                if len(tiepNode) < 1:   break
                next = mechanize.urljoin(url, getAttributeText(tiepNode[0], 'href'))
                print ">> go to next page: {0}".format(next)
                if compareURL(url, next): break
                url = next
                
        except:
            err = "{0} - {1} >> {2}".format(q, pg, sys.exc_info()[0])
            print err
            raise
        
        return json.dumps({})
        
    
if __name__ == '__main__':
    
#    img = ImagesGoogle()
#    img.getimg('longhoanggiang', 2)
#    
    global_conf = {
        'global': {
        'server.socket_host': '127.0.0.1',
        'server.socket_port': 8889,
        'tools.encode.on': True,
        'tools.encode.encoding': 'UTF-8'
        },
    }
    cherrypy.config.update(global_conf)
    cherrypy.quickstart(ImagesGoogle())
    print ">> Finished at {0}".format(str(datetime.datetime.now()))
    sys.exit()